<template>
  <div class="topic-main">
    <head-top head-title="发表话题" topic-publish="发布" goBack="true" @topicPublish="addTopic"></head-top>
    <publish @getValue="getValue" @getImage="getImage" :holder="holder"></publish>
  </div>
</template>

<script>
  import headTop from '../header/Header.vue'
  import { Toast, Indicator } from 'mint-ui'
  import qs from 'qs'
  import {
    loadVillageId,
    loadOpenId
  } from '../../common/js/cache.js'
  import commonUrl from '../../common/js/commonUrl.js'
  import publish from '../common/publish.vue'

  export default {
    name: 'Topic',
    data () {
      return {
        topicDetail: '',
        serverIds: [],
        showLoading: false,
        holder: '请输入您要发布的话题内容(字数不超过200)'
      }
    },
    components: {
      headTop,
      publish
    },
    created() {
    },
    methods: {
      getValue (value) {
        this.topicDetail = value
      },
      getImage (image) {
        // this.imagesLrz = image
        this.serverIds = image
      },
      addTopic () {
        setTimeout(function () {
          Indicator.close()
        }, 5000)
        if (!this.topicDetail.trim().length || !this.topicDetail) {
          Toast({
            message: '文本内容不能为空',
            position: 'middle',
            duration: 2000
          })
          return
        }
        Indicator.open({
          text: '发布中...',
          spinnerType: 'fading-circle'
        })
        const url = `${commonUrl.apihost}index.php/home/forum/add`
        const data = {
          content: this.topicDetail,
          serverIds: this.serverIds,
          areas_id: loadVillageId(),
          openid: loadOpenId()
        }
        this.$http.post(url, qs.stringify(data), {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(res => {
          if (res.data.code === 200) {
            Indicator.close()
            Toast({
              message: '发布成功',
              position: 'middle',
              duration: 2000
            })
            history.replaceState({}, '', '/Home')
            this.$router.push('/Message')
          }
          if (res.data.code === 400) {
            Indicator.close()
            Toast({
              message: '发布失败',
              position: 'middle',
              duration: 1500
            })
          }
          if (res.data.code === 401) {
            Indicator.close()
            Toast({
              message: '您的发表帖子已经触犯相关法律法规,暂时不能使用发帖和评论功能.',
              position: 'middle',
              duration: 1500
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .topic-main{
    padding-top: 40px;
  }
</style>
